/*
 * Copyright 2022-2022 Prat Tana
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gitee.wsl.common.ui.draw

import androidx.compose.runtime.Composable
import androidx.compose.runtime.ComposeNode
import androidx.compose.ui.graphics.vector.DefaultRotation
import androidx.compose.ui.graphics.vector.DefaultScaleX
import androidx.compose.ui.graphics.vector.DefaultScaleY
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.draw.container.GroupNode
import com.gitee.wsl.common.ui.draw.node.ElementNode
import com.gitee.wsl.common.ui.draw.node.Node

@Composable
fun CollageGroup(
    rotation: Float = DefaultRotation,
    pivotX: Dp = 0.dp,
    pivotY: Dp = 0.dp,
    scaleX: Float = DefaultScaleX,
    scaleY: Float = DefaultScaleY,
    translationX: Dp = 0.dp,
    translationY: Dp = 0.dp,
    content: @Composable () -> Unit
) {
    val (pX, pY) = with(LocalDensity.current) {
        pivotX.toPx() to pivotY.toPx()
    }
    val (x, y) = with(LocalDensity.current) {
        translationX.toPx() to translationY.toPx()
    }
    ComposeNode<GroupNode, DrawApplier>(
        factory = { GroupNode() },
        update = {
            set(rotation) { this.rotation = it }
            set(pivotX) { this.pivotX = pX }
            set(pivotY) { this.pivotY = pY }
            set(scaleX) { this.scaleX = it }
            set(scaleY) { this.scaleY = it }
            set(translationX) { this.translationX = x }
            set(translationY) { this.translationY = y }
        }
    ) {
        content()
    }
}


@Composable
fun CollageElements(
    nodes: List<Node>
) {
    ComposeNode<ElementNode, DrawApplier>(
        factory = { ElementNode() },
        update = {
            set(nodes) { this.nodes = it }
        }
    )
}

